package com.flyee.viewer.menu
{
	import com.flyee.controller.UIController;
	import com.flyee.events.LoaderEvent;
	import com.flyee.model.MenuData;
	import com.flyee.service.pool.ObjectCollection;
	import com.flyee.utils.Constants;
	import com.flyee.utils.DLLLoader;
	import com.flyee.utils.XMLLoader;
	import com.flyee.viewer.*;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.NetStatusEvent;
	
	import mx.core.UIComponent;

	public class RealtyOption extends EventDispatcher
	{
		private var mainItems:Array = null;
		private var hypoItems:Array = null;

		private var xmlLoader:XMLLoader = null;
		private var parser:MainMenuXMLParser = null;
		private var uiController:UIController = null;

		public function RealtyOption()
		{
			uiController = ObjectCollection.beansContext.getObject("uiController") as UIController;
		}

		private function completeHandler(e:LoaderEvent):void
		{
			var data:String = e.getTxtFile();
			parser = new MainMenuXMLParser();
			mainItems = new Array();
			hypoItems = new Array();

			parser.parse(data);
			mainItems = parser.getAllMainItems();
			hypoItems = parser.getAllHypoItems();
			this.dispatchEvent(new Event(Event.COMPLETE));
		}

		public function xmlLoad(url:String):void
		{
			//var uri:String = Configuration.staticServer + "/shareDepot/uiconfig.xml";
			//var uri:String = "com/goldcitynet/system/community.xml";
			var uri:String = url;
			xmlLoader = new XMLLoader(uri);
			xmlLoader.addEventListener(LoaderEvent.COMPLETE_LOADER, completeHandler);
		}

		public function getListAt(tag:int):Array
		{

			if (tag < 7 && tag > 0)
			{
				var temp:Array = hypoItems[tag - 1];
				return temp
			}
			return null;
		}

		public function getMainList():Array
		{
			var leng:int = mainItems.length;
			var temp:Array = new Array(leng);

			for(var i:int = 0; i < leng; i++)
			{
				var model:MenuData = MenuData(mainItems[i]);
				temp[i] = model;
			}
			return temp;
		}

		public function getIconNameList():Array
		{
			var leng:int = mainItems.length;
			var temp:Array = new Array(leng);

			for(var i:int = 0; i < leng; i++)
			{
				var model:MenuData = MenuData(mainItems[i]);
				temp[i] = model.itemIcon;
			}
			return temp;
		}

		public function openWindow(target:String):void
		{
			var panelName:String = null;
			switch(target){
				case Constants.VIEWER_SANDTABLE_WINDOW:
					panelName = SandTableViewer.NAME;
					break;
				case "":
					break;
				case "":
					break;
				default:
					break;
			} 
			var window:UIComponent = uiController.getPanelByName(panelName);
			if(window){
				uiController.removedFromDesktop(window);
			}else{
				uiController.addToDesktopByName(panelName);
			}
		}
		
		private function fastLoadPv3dDll(obj:Object):void{
			var loader:DLLLoader = new DLLLoader();
            loader.addEventListener(DLLLoader.ALL_COMPLETED, pv3dInit);
            loader.addDLL("resource/PV3DDll_1.7.swf", "pv3d 1.7");

			if(obj){
				loader.setDataObject(obj);
			}
            loader.notify();

		}
		
		private function pv3dInit(e:NetStatusEvent):void{
			var obj:Object = e.info;
		}

		public function clear():void
		{
			//factory = null;

			if (mainItems)
			{
				mainItems.length = 0;
			}
			mainItems = null;

			if (hypoItems)
			{
				hypoItems.length = 0;
			}
			hypoItems = null;
			xmlLoader = null;
			parser.clear();
			parser = null;
		}

	}
}

